package com.rem.chat.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.rem.chat.pojo.dataobject.UserContactDO;
import com.rem.chat.pojo.response.LoadContactResponse;
import com.rem.chat.service.dto.ContactInfoDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author: Themberfue
 * @date: 2025/6/5 22:03
 * @description:
 */
@Mapper
public interface UserContactMapper extends BaseMapper<UserContactDO> {
    /**
     * 查询群组成员
     */
    @Select("SELECT c.user_id, c.contact_id, c.create_time, u.gender, u.nick_name " +
            "FROM user_contact c " +
            "INNER JOIN user_info u ON c.user_id = u.user_id " +
            "${ew.customSqlSegment} " +
            "ORDER BY c.create_time DESC")
    List<ContactInfoDTO> selectContactInfo(@Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 查询用户的好友列表
     */
    @Select("SELECT " +
            "c.contact_id, " +
            "u.nick_name AS contact_name " +
            "FROM " +
            "user_contact c " +
            "INNER JOIN user_info u ON u.user_id = c.contact_id " +
            "WHERE " +
            "c.user_id = #{userId} " +
            "AND c.`status` IN (1, 3, 5) " +
            "AND c.contact_type = 0 " +
            "ORDER BY c.create_time DESC")
    List<LoadContactResponse> selectUserFriends(@Param("userId") String userId);

    /**
     * 查询用户的群组列表
     */
    @Select("SELECT " +
            "c.contact_id, " +
            "g.group_name AS contact_name " +
            "FROM " +
            "user_contact c " +
            "INNER JOIN group_info g ON g.group_id = c.contact_id " +
            "AND g.group_owner_id != c.user_id " +
            "WHERE " +
            "c.user_id = #{userId} " +
            "AND c.`status` IN (1, 3, 5) " +
            "AND c.contact_type = 1 " +
            "ORDER BY c.create_time DESC")
    List<LoadContactResponse> selectUserGroups(@Param("userId") String userId);
}
